package com.huawei.controller;

import com.alibaba.fastjson.JSON;
import com.huawei.entity.Dingdan;
import com.huawei.service.DingdanService;
import com.huawei.util.Result;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DingdanController
 * @Description TODO
 * @Author 马效鹏
 * @Date 2021/11/29 18:22
 */
@Controller
@RequestMapping("/Dingdan")
public class DingdanController {
    @Resource
    DingdanService dingdanImpl;
    @RequestMapping(value = "/chaxun", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String chaxun(
                         @RequestParam(value = "dingdan_id", required = false) Integer dingdan_id,
                         @RequestParam(value = "name", required = false) String name,
                         @RequestParam("page") int page,
                         @RequestParam("limit") int limit) {
        Map<String, Object> pars = new HashMap<>();
        pars.put("dingdan_id",dingdan_id);
        pars.put("name",name);
        pars.put("offset", (page-1)*limit);
        pars.put("limit", limit);

        List<Dingdan> list = dingdanImpl.selectAll(pars);
        int count =dingdanImpl.count(pars);
        Result result = new Result(0, "查询成功", count, list);
        return JSON.toJSONString(result);

    }

    @RequestMapping(value = "/add", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String add(Dingdan dingdan) {

        int n = dingdanImpl.add(dingdan);
        if (n == 1) {
            return "添加成功";
        } else {
            return "添加失败";
        }

    }

    @RequestMapping(value = "/update", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String update( Dingdan dingdan) {

        int n = dingdanImpl.update(dingdan);
        if (n == 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }

    }
    @RequestMapping(value = "/delete", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String delete(int dingdan_id){
        int n= dingdanImpl.delete(dingdan_id);
        if (n == 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }

    }



    @RequestMapping(value = "/updates", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updates( @RequestParam("dingdan_id")int dingdan_id,
                           @RequestParam("shangpin_status") String shangpin_status) {

        int n = dingdanImpl.status(dingdan_id,shangpin_status);
        if (n == 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }

    }
}
